<?php
require_once('../../config.php');
Doctrine::loadModels('../../models');

// can combine agent and people into query to return one result set.
// wrote SQL select as unsure of doctrine method when multiple references to same table
if ($_REQUEST["agent_id"]) 
{
	$agent_id = strip_tags($_REQUEST["agent_id"]);
	//file_put_contents  ( "param_id.txt", $agent_id);	
	
	$query = $db_query = sprintf("SELECT a.agent_id, a.name_short, a.name_full, a.agent_type_id, a.description, a.date_start, a.date_end,
        c.membership_type_id, c.status_id, c.continent_id, c.region_id, c.subregion_id, 
        c.development_rating_id, c.mou_signed_by_id, c.country_iso_code_3, c.country_iso_code_2, c.sign_mou_2001, c.sign_mou_2007,  
        continent.item_name continent, r.item_name region, s.item_name subregion, m.item_name membership_type, cs.item_name gbif_status,
        d.item_name development_rating, sb.name_short s_first_name, sb.name_full s_last_name 
	FROM countries_participants c
  		INNER JOIN agent a ON c.agent_country_id = a.agent_id
  		INNER JOIN lk_glossary continent ON c.continent_id = continent.item_id
  		INNER JOIN lk_glossary r ON c.region_id = r.item_id
  		INNER JOIN lk_glossary s ON c.subregion_id = s.item_id 
  		INNER JOIN lk_glossary m ON c.membership_type_id = m.item_id
  		INNER JOIN lk_glossary cs ON c.status_id = cs.item_id
  		INNER JOIN lk_glossary d ON c.development_rating_id = d.item_id
  		INNER JOIN agent sb ON c.mou_signed_by_id = sb.agent_id
	WHERE a.agent_id ='".$agent_id."'");
	
	$items = mysql_query($query);	
}

$dom = new DomDocument('1.0',"UTF-8");
$dom->formatOutput = true;
$root = $dom->createElement( "results" );
$dom->appendChild( $root );
	while(($item = mysql_fetch_assoc($items)))
	{
		$dn = $dom->createElement( "record" );
		$dn->setAttribute( 'agent_id', ($item['agent_id']));
		$dn->setAttribute( 'name_short', ($item['name_short']));
		$dn->setAttribute( 'name_full', ($item['name_full']));
		$dn->setAttribute( 'description', ($item['description']));
		$dn->setAttribute( 'date_start', $item['date_start'] );
		$dn->setAttribute( 'date_end', $item['date_end'] );
		$dn->setAttribute( 'membership_type_id', $item['membership_type_id'] );
		$dn->setAttribute( 'status_id', $item['status_id'] );
		$dn->setAttribute( 'continent_id', $item['continent_id'] );
		$dn->setAttribute( 'region_id', $item['region_id']);
		$dn->setAttribute( 'subregion_id', ($item['subregion_id']));
		$dn->setAttribute( 'development_rating_id', ($item['development_rating_id']));
		$dn->setAttribute( 'mou_signed_by_id', $item['mou_signed_by_id'] );
		$dn->setAttribute( 'country_iso_code_3', $item['country_iso_code_3'] );
		$dn->setAttribute( 'country_iso_code_2', $item['country_iso_code_2'] );
		$dn->setAttribute( 'sign_mou_2001', $item['sign_mou_2001'] );
		$dn->setAttribute( 'sign_mou_2007', $item['sign_mou_2007'] );
		$dn->setAttribute( 'continent', $item['continent'] );
		$dn->setAttribute( 'region', $item['region'] );
		$dn->setAttribute( 'subregion', $item['subregion'] );
		$dn->setAttribute( 'membership_type', $item['membership_type'] );
		$dn->setAttribute( 'gbif_status', $item['gbif_status'] );
		$dn->setAttribute( 'development_rating', $item['development_rating'] );
		$dn->setAttribute( 'mou_signed_by', ($item['s_last_name'].', '.$item['s_first_name']));
		$root->appendChild( $dn );
	}		
header( "Content-type: text/xml; charset=UTF-8");
echo $dom->saveXML();

?>